/*
* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const { XButton } = require("../../engine/control/XButton");
const { X2DBeta } = require("../../engine/graphics/X2DInterface");
const { Scr } = require("../../engine/XDefine");
const { XTools } = require("../../engine/XTools");
const { XStat } = require("../../engine/XStat");
const { XWindow } = require("../../engine/control/XWindow");
const { PackageTool } = require("../ros_msg_utils/PackageTool");
const { WsClient2 } = require("../../engine/ws/WsClient2");
const { RuntimeManager } = require("./0/RuntimeManager");
const { RosProto } = require("../ros_msg_utils/RosProto");
const { ProjectConfig } = require("./ProjectConfig");
const { LogManager } = require("./LogManager");
const { CustomManager } = require("./1/CustomManager");
const { AttributeArea } = require("../AttributeArea");
const { RobotManager } = require("../tab3simulate/RobotManager");

class EditRos1Project extends XWindow {
    constructor() {
        super("EditRos1Project", true);

        this.loading = true;
        WsClient2.registProcess("get_ros_state_ret", (pt_ret) => {
            let success = pt_ret.get_string();
            if (success == "ok") {
                let nodeCount = pt_ret.get_uint32();
                let rosState = {};
                for (let i = 0; i < nodeCount; i++) {
                    let nn = pt_ret.get_string();//node name
                    rosState[nn] = {}
                    rosState[nn]["pub"] = pt_ret.get_string_array();
                    rosState[nn]["sub"] = pt_ret.get_string_array();
                    rosState[nn]["srv"] = pt_ret.get_string_array();
                }
                RuntimeManager.getInstance().expand(rosState);
            }
        });
        ProjectConfig.getInstance().loadConfig(() => {
            RosProto.getRosState();
            RosProto.collectMessages();
            RosProto.echo();
            this.loading = false;
        });
        LogManager.I(`*** 欢迎使用Maestro机器人可视化编辑器 ***
了解更多信息，请访问：https://gitee.com/li-tao116/maestro
`);
        this.workspaceBtns = [
            new XButton({ name: "运行时" }),
            new XButton({ name: "自定义节点和消息" }),
            new XButton({ name: "三方库管理" }),
            new XButton({ name: "仿真机器人构建" }),
        ];
        CustomManager.getInstance();
        this.workspacePoint = 0;
        AttributeArea.gi().hide();
    }

    drawWorkspaceBtns() {
        let w = 150;
        let h = 20;
        let y = 0;
        for (let i = 0; i < this.workspaceBtns.length; i++) {
            let btn = this.workspaceBtns[i];
            let x = Scr.logicw / 2 + (i - 2) * w;
            btn.move(x, y, w, h);
            if (i == this.workspacePoint) {
                X2DBeta.fillRect(x + 1, y, w - 2, h, 0xff800000);
            } else {
                X2DBeta.fillRect(x + 1, y, w - 2, h, 0xff400000);
            }

            X2DBeta.drawText(btn.name_, 14, x + w / 2, y + h / 2, 1, 1, 0, -2, -2, 0xffffffff);
        }
    }

    onDraw() {
        if (this.loading) {
            X2DBeta.drawText("Loading...", 24, Scr.logicw / 2, Scr.logich / 2, 1, 1, 0, -2, -2, 0xffffffff);
            return;
        }
        // X2DBeta.drawText(ProjectConfig.PROJECT_NAME, 24, Scr.logicw / 2, 0, 1, 1, 0, -2, -1, 0xffffffff);

        switch (this.workspacePoint) {
            case 0://运行时
                RuntimeManager.getInstance().onDraw();
                break;
            case 1://自定义节点和消息
                CustomManager.getInstance().onDraw();
                break;
            case 2://三方库管理
                break;
            case 3://仿真机器人构建]
                RobotManager.getInstance().onDraw();
                break;
        }


        LogManager.onDraw();

        this.drawWorkspaceBtns();
    }
    onTouch(msg, x, y) {
        if (this.loading) {
            return true;
        }
        if (LogManager.onTouch(msg, x, y)) {
            return true;
        }
        for (let btn of this.workspaceBtns) {
            if (btn.onTouch(msg, x, y)) {
                if (btn.IsClicked()) {
                    this.workspacePoint = this.workspaceBtns.indexOf(btn);
                    if (this.workspacePoint == 1) {
                        CustomManager.getInstance().resetEditing(CustomManager.getInstance().editing, true);
                        AttributeArea.gi().show();
                    } else if (this.workspacePoint == 3) {
                        RobotManager.getInstance().reset("main", true);
                        AttributeArea.gi().show();
                    } else {
                        AttributeArea.gi().hide();
                    }
                }
                return true;
            }
        }
        switch (this.workspacePoint) {
            case 0://运行时
                if (RuntimeManager.getInstance().onTouch(msg, x, y)) {
                    return true;
                }
                break;
            case 1://自定义节点和消息
                if (CustomManager.getInstance().onTouch(msg, x, y)) {
                    return true;
                }
                break;
            case 2://三方库管理
                break;
            case 3://仿真机器人构建
                if (RobotManager.getInstance().onTouch(msg, x, y)) {
                    return true;
                }
                break;
        }
        return true;
    }
    onKey(type, k) {
        if (this.loading) {
            return true;
        }
        if (RuntimeManager.getInstance().onKey(type, k)) {
            return true;
        }
        return true;
    }
    static open() {
        XStat.pushStat(new EditRos1Project());
    }
}

module.exports = {
    EditRos1Project
}